package com.tabjin.rfidsocket.controller.v1.access;

import com.tabjin.rfidsocket.common.RequestHolder;
import com.tabjin.rfidsocket.common.utils.PagedGridResult;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.exception.ParamException;
import com.tabjin.rfidsocket.pojo.ArchProject;
import com.tabjin.rfidsocket.pojo.ArchProjectBuildingUser;
import com.tabjin.rfidsocket.pojo.bo.access.ArchProjectBO;
import com.tabjin.rfidsocket.pojo.vo.access.ArchProjectVO;
import com.tabjin.rfidsocket.service.access.ArchProjectService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author tabjin
 * create at 2021/1/11 13:59
 * @program rfid_socket
 * @description 门禁-项目
 */
@RestController
@RequestMapping(value = "/access/project")
public class ProjectController {
    private final ArchProjectService archProjectService;

    public ProjectController(ArchProjectService archProjectService) {
        this.archProjectService = archProjectService;
    }

    /**
     * 新增项目
     *
     * @param bo
     * @return
     */
    @RequiresPermissions(value = {"/access/project/add"}, logical = Logical.OR)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult add(@RequestBody ArchProjectBO bo) {
        ArchProjectVO result = archProjectService.add(bo);
        return TabJsonResult.ok(result);
    }

    /**
     * 删除项目
     *
     * @param projectId
     * @return
     */
    @RequiresPermissions(value = {"/access/project/delete"}, logical = Logical.OR)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult delete(@RequestParam(value = "projectId") Integer projectId) {
        ArchProject archProject = archProjectService.delete(projectId);
        return TabJsonResult.ok(archProject);
    }

    /**
     * 更新项目
     *
     * @param bo
     * @return
     */
    @RequiresPermissions(value = {"/access/project/update"}, logical = Logical.OR)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult update(@RequestBody ArchProjectBO bo) {
        ArchProjectVO result = archProjectService.update(bo);
        return TabJsonResult.ok(result);
    }

    /**
     * 获取所有项目
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequiresPermissions(value = {"/access/project/list"}, logical = Logical.OR)
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult getAll(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        PagedGridResult result = archProjectService.getAll(page, pageSize);
        return TabJsonResult.ok(result);
    }

    /**
     * 项目查询过滤
     *
     * @param page
     * @param pageSize
     * @param province
     * @param city
     * @param projectName
     * @return
     */
    @RequiresPermissions(value = {"/access/project/list"}, logical = Logical.OR)
    @RequestMapping(value = "/filter", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult projectsFilter(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                        @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                        @RequestParam(value = "province", required = false) String province,
                                        @RequestParam(value = "city", required = false) String city,
                                        @RequestParam(value = "name", required = false) String projectName) {
        PagedGridResult pagedGridResult = archProjectService.projectsFilter(page, pageSize, province, city, projectName);
        return TabJsonResult.ok(pagedGridResult);
    }

    /**
     * 获取项目详情
     *
     * @param projectId
     * @return
     */
    @RequiresPermissions(value = {"/access/project/list"}, logical = Logical.OR)
    @RequestMapping(value = "/item", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult item(@RequestParam(value = "id", required = true) Integer projectId) {
        ArchProjectVO result = archProjectService.getEachProject(projectId);
        return TabJsonResult.ok(result);
    }

    @RequiresPermissions(value = {"/access/project/list"}, logical = Logical.OR)
    @RequestMapping(value = "/project/by/user", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult projectByUser() {
        Integer id = RequestHolder.getCurrentUser().getId();
        Optional<List<ArchProject>> projectByManagerIdOptional = archProjectService.getProjectByManagerId(RequestHolder.getCurrentUser().getId());
        List<ArchProject> archProjectList = projectByManagerIdOptional.orElseThrow(() -> new ParamException("无项目"));
        return TabJsonResult.ok(archProjectList);
    }


    /**
     * 根据县区获取项目
     *
     * @param region
     * @return
     */
    @RequestMapping(value = "/projects/region", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult projectsByDistrict(@RequestParam(value = "region", required = true) String region) {
        Optional<List<ArchProject>> projectsByRegionOptional = archProjectService.getProjectsByRegion(region);
        List<ArchProject> result = projectsByRegionOptional.orElseThrow(() -> new ParamException("无项目"));
        return TabJsonResult.ok(result);
    }
}
